<?php
session_start();
//Tiina Võsumägi phpkursus1109 14.11
echo "<h3>Sisesta üks number 1-10-ni</h1>";

if (isset($_GET['uus'])) 
{
    $_SESSION['arv'] = rand(1, 10);
}
if (isset($_GET['cancel'])) 
{
    $arv = $_SESSION['arv'];
    unset($_SESSION['arv']);
    unset($_SESSION['loe']);
    session_destroy();
}

echo '<a href="?uus">Uus mäng </a>';
if ($_SESSION['arv'])
    echo '&nbsp;|&nbsp;<a href="?cancel">Katkesta mäng</a>';
echo "<br><br>";
if ($_SESSION['arv']) 
{
    if (!$_SESSION['loe'])
        $_SESSION['loe'] = 0;
    echo '<form action="?" method="POST"<br>
        <input name="number" type="text">
        <input type="submit" name="paku" value="Paku">
        </form><br>';
    if (isset($_POST['paku'])) 
	{
        if ($_POST['number'] == '')
            echo 'Number jäi sisestamata!';
        else if (!is_numeric($_POST['number']))
            echo 'Sisesta ainult numbreid!';
        else {
            if ($_POST['number'] != $_SESSION['arv']) 
			{
                echo ($_POST['number'] < $_SESSION['arv'])?
				'Number on suurem!':'Number on väiksem!';
                $_SESSION['loe']++;
            }
            else {
                echo 'Õige, ära arvasid! <br>';
                echo 'See number oli: '.$_SESSION['arv'].'<br>';
                echo 'Sa pakkusid '.$_SESSION['loe'].' korda!';
                    $arv = $_SESSION['arv'];
                    unset($_SESSION['arv']);
                    unset($_SESSION['loe']);
                    session_destroy();
            }

        }
    }
}
else if (isset($_GET['cancel'])) 
echo ' Eelmise mängu vastus oli: '.$arv;
else echo ' Mine mängima!';
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
